
package com.thb.pay.core.holder;

import com.thb.pay.core.logs.utils.LogConstants;

import java.util.HashMap;
import java.util.Map;

public abstract class LogContextHolder {

    private static final ThreadLocal<Map<String, Object>> THREAD_LOCAL_CONTEXTS = new ThreadLocal<>();

    public static Map<String, Object> currentContextMap() {
        Map<String, Object> context = THREAD_LOCAL_CONTEXTS.get();

        if (context == null) {
            context = initContextMap();
        }
        return context;
    }

    protected static Map<String, Object> initContextMap() {
        Map<String, Object> context = new HashMap<>();
        THREAD_LOCAL_CONTEXTS.set(context);
        return context;
    }

    public static boolean has(String key) {
        return currentContextMap().containsKey(key);
    }

    protected static <T> T get(String key, Class<T> valueClass) {
        Map<String, Object> currentContextMap = currentContextMap();
        if (currentContextMap.containsKey(key)) {
            Object o = currentContextMap.get(key);
            if (valueClass.isInstance(o)) {
                return valueClass.cast(o);
            }
        }
        return null;
    }

    protected static <T> void set(String key, T value) {
        Map<String, Object> context = currentContextMap();
        if (context.containsKey(key)) {
            context.remove(key);
        }

        context.put(key, value);
    }

    protected static void delete(String key) {
        Map<String, Object> currentContextMap = currentContextMap();
        if (currentContextMap.containsKey(key))
            currentContextMap.remove(key);
    }

    public static void removeCurrentContextMap() {
        THREAD_LOCAL_CONTEXTS.remove();
    }

    public static boolean existLogReqId() {
        return has(LogConstants.LOG_REQ_ID);
    }

    public static void setLogReqId(String logReqId) {
        set(LogConstants.LOG_REQ_ID, logReqId);
    }

    public static String getLogReqId() {
        return get(LogConstants.LOG_REQ_ID, String.class);
    }

    public static void removeLogReqId() {
        delete(LogConstants.LOG_REQ_ID);
    }


    public static boolean existReqTimeId() {
        return has(LogConstants.REQ_TIME_ID);
    }

    public static void setReqTimeId(Long userId) {
        set(LogConstants.REQ_TIME_ID, userId);
    }

    public static Long getReqTimeId() {
        return get(LogConstants.REQ_TIME_ID, Long.class);
    }

    public static void removeTimeId() {
        delete(LogConstants.REQ_TIME_ID);
    }


    public static void setFileKey(String fileKey) {
        set(LogConstants.FILE_KEY, fileKey);
    }

    public static String getFileKey() {
        return get(LogConstants.FILE_KEY, String.class);
    }

    public static void removeFileKey() {
        delete(LogConstants.FILE_KEY);
    }


    public static void setActionId(String fileKey) {
        set(LogConstants.MDC_ACTION, fileKey);
    }

    public static String getActionId() {
        return get(LogConstants.MDC_ACTION, String.class);
    }

    public static void removeActionId() {
        delete(LogConstants.MDC_ACTION);
    }
}
